﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;

namespace mat2tab_fun.functionPlots
{
    class TrigonometricFunctionPlot : FunctionPlot
    {
        public TrigonometricFunctionPlot(bool testMonotonicznosci, Grid plotGrid, int size, String trFunType, int a, int b, int c)
            : base(testMonotonicznosci, plotGrid, size)
        {
            if (c != 0)
            {
                double delta = 0.05;
                bool inf = false;
                double nextX = 0, nextY = 0;
                for (double x = -halfX / size; x <= halfX / size; )
                {
                    Line l = new Line();
                    if (inf)
                    {
                        inf = false;
                        l.X1 = nextX;
                        l.Y1 = nextY;
                    }
                    else
                    {
                        l.X1 = x * size + halfX;
                        if (trFunType.Equals("SIN"))
                            l.Y1 = a * Math.Pow(Math.Sin(c * x), b) * size;
                        else if (trFunType.Equals("COS"))
                            l.Y1 = a * Math.Pow(Math.Cos(c * x), b) * size;
                        else if (trFunType.Equals("TAN"))
                            l.Y1 = a * Math.Pow(Math.Tan(c * x), b) * size;
                        else if (trFunType.Equals("CTAN"))
                            l.Y1 = 1 / (a * Math.Pow(Math.Tan(c * x), b)) * size;

                        if (l.Y1 >= 0)
                            l.Y1 = halfY - l.Y1;
                        else
                            l.Y1 = l.Y1 * (-1) + halfY;
                    }

                    x += delta;

                    l.X2 = x * size + halfX;
                    if (trFunType.Equals("SIN"))
                        l.Y2 = a * Math.Pow(Math.Sin(c * x), b) * size;
                    else if (trFunType.Equals("COS"))
                        l.Y2 = a * Math.Pow(Math.Cos(c * x), b) * size;
                    else if (trFunType.Equals("TAN"))
                        l.Y2 = a * Math.Pow(Math.Tan(c * x), b) * size;
                    else if (trFunType.Equals("CTAN"))
                        l.Y2 = 1 / (a * Math.Pow(Math.Tan(c * x), b)) * size;

                    if (l.Y2 >= 0)
                        l.Y2 = halfY - l.Y2;
                    else
                        l.Y2 = l.Y2 * (-1) + halfY;

                    if (testMonotonicznosci)
                    {
                        if (l.Y1 > l.Y2)
                            l.Stroke = Brushes.Red;
                        else
                            l.Stroke = Brushes.Green;
                    }
                    else
                        l.Stroke = Brushes.Blue;

                    if (Math.Abs(l.Y1 - l.Y2) < 350)
                        lines.Add(l);
                    else
                    {
                        Line l2 = new Line();
                        l2.Y1 = l.Y1;
                        l2.X1 = l.X1;
                        l2.X2 = l.X2;

                        //dodanie punktów w nieskonczonosci
                        if (l2.Y1 < 350)
                            l2.Y2 = -10000;
                        else
                            l2.Y2 = 10000;

                        if (testMonotonicznosci)
                        {
                            if (l2.Y1 > l2.Y2)
                                l2.Stroke = Brushes.Red;
                            else
                                l2.Stroke = Brushes.Green;
                        }
                        else
                            l2.Stroke = Brushes.Blue;

                        lines.Add(l2);

                        if (l.Y2 < 350)
                            l.Y1 = -10000;
                        else
                            l.Y1 = 10000;
                        if (testMonotonicznosci)
                        {
                            if (l.Y1 > l.Y2)
                                l.Stroke = Brushes.Red;
                            else
                                l.Stroke = Brushes.Green;
                        }
                        else
                            l.Stroke = Brushes.Blue;
                        lines.Add(l);
                    }
                }
                DrawPlot();
            }
        }
    }
}
